package com.dw.controller;

import cn.hutool.core.lang.UUID;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dw.entity.OrderInfo;
import com.dw.feign.ProductFeign;
import com.dw.service.OrderService;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    ProductFeign productFeign;

    // 最大文件大小（10MB）
    private static final long MAX_FILE_SIZE = 10 * 1024 * 1024;

    // 允许的文件类型
    private static final List<String> ALLOWED_EXTENSIONS = Arrays.asList(
            ".jpg", ".jpeg", ".png", ".gif", ".pdf", ".doc", ".docx", ".txt"
    );

    /**
     * minio上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        if(file.getSize()>MAX_FILE_SIZE){
            throw new IllegalAccessException("文件过大");
        }
        String ext = file.getOriginalFilename().substring( file.getOriginalFilename().lastIndexOf("."));
        if (!ALLOWED_EXTENSIONS.contains(ext)) {
            throw new IllegalArgumentException("不支持的文件类型，仅支持: " + ALLOWED_EXTENSIONS);
        }
        return orderService.uploadFile(file);
    }

    /**
     * 获取图片连接
     * @param fileName
     * @return
     */
    @GetMapping("/getUrl")
    public String getFileUrl(@RequestParam("filename") String fileName){
        return orderService.getFileUrl(fileName, 30);
    }
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name") String name) { //http://localhost:9091/order/hello?name=daiwei
        System.out.println("===订单模块:hello order==="+name);
        return "===订单模块:hello order==="+name;
    }

    @GetMapping("/makeOrder")
    public String makeOrder() throws InterruptedException {  //http://localhost:9091/order/makeOrder
        //String str = restTemplate.getForObject("http://localhost:9091/product/reduceProduct", String.class);
        //String str = restTemplate.getForObject("http://product-module/product/reduceProduct", String.class);
        //Thread.sleep(10000);
        orderService.makeOrder();
        return "===订单下单成功:makeOrder---------------";
    }


    @GetMapping("/makeOrder2")
    @SentinelResource(value = "flow",blockHandler = "flowBlockHandler")
    public String makeOrder2() throws InterruptedException {  //http://localhost:9091/order/makeOrder
        //String str = restTemplate.getForObject("http://localhost:9091/product/reduceProduct", String.class);
        //String str = restTemplate.getForObject("http://product-module/product/reduceProduct", String.class);
        //Thread.sleep(10000);
        orderService.makeOrder();
        return "===订单下单成功:makeOrder---------------";
    }
    public String flowBlockHandler(BlockException e){
        return "===流控成功===";
    }
}
